public class LinkedList {
    public static class ListNode {
        private int val;
        private ListNode next;
        private ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;
    private int usedSize; // 记录链表节点数量

    public LinkedList() {
        this.usedSize = 0;
    }

    // 头插法
    public void addFirst(int data) {
        ListNode cur = new ListNode(data);
        if (head == null) {
            head = cur;
            last = cur;
        } else {
            cur.next = head;
            head.prev = cur;
            head = cur;
        }
        usedSize++;
    }

    // 尾插法
    public void addLast(int data) {
        ListNode cur = new ListNode(data);
        if (head == null) {
            head = cur;
            last = cur;
        } else {
            last.next = cur;
            cur.prev = last;
            last = cur;
        }
        usedSize++;
    }

    // 任意位置插入，第一个数据节点为 0 号下标
    public boolean addIndex(int index, int data) {
        if (index < 0 || index > usedSize) {
            System.out.println("插入位置不合法");
            return false;
        }
        ListNode newNode = new ListNode(data);
        if (index == 0) {
            // 插入到头部
            if (head == null) {
                head = newNode;
                last = newNode;
            } else {
                newNode.next = head;
                head.prev = newNode;
                head = newNode;
            }
        } else if (index == usedSize) {
            // 插入到尾部
            last.next = newNode;
            newNode.prev = last;
            last = newNode;
        } else {
            // 插入到中间位置
            ListNode cur = head;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            newNode.prev = cur.prev;
            newNode.next = cur;
            cur.prev.next = newNode;
            cur.prev = newNode;
        }
        usedSize++;
        return true;
    }

    // 查找是否包含关键字 key 是否在双向链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 删除第一次出现关键字为 key 的节点
    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur.prev == null) {
                    // 删除的是头节点
                    head = cur.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else if (cur.next == null) {
                    // 删除的是尾节点
                    last = cur.prev;
                    last.next = null;
                } else {
                    // 删除的是中间节点
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                usedSize--;
                return;
            }
            cur = cur.next;
        }
    }

    // 删除所有值为 key 的节点
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                ListNode nextNode = cur.next;
                if (cur.prev == null) {
                    // 删除的是头节点
                    head = cur.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else if (cur.next == null) {
                    // 删除的是尾节点
                    last = cur.prev;
                    last.next = null;
                } else {
                    // 删除的是中间节点
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                usedSize--;
                cur = nextNode;
            } else {
                cur = cur.next;
            }
        }
    }

    // 得到双向链表的长度
    public int size() {
        return usedSize;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode nextNode = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = nextNode;
        }
        head = null;
        last = null;
        usedSize = 0;
    }
}

